package com.wobito.controller.operate;

import com.wobito.common.constants.BaseResponse;
import com.wobito.common.constants.SysConstants;
import com.wobito.common.utils.DateUtils;
import com.wobito.common.utils.ResultUtils;
import com.wobito.pojo.bean.CabinetLat;
import com.wobito.pojo.bean.DeviceLog;
import com.wobito.pojo.vo.cabinetLat.AdminCloseLatVO;
import com.wobito.pojo.vo.cabinetLat.AdminOpenLatVO;
import com.wobito.service.BookService;
import com.wobito.service.CabinetLatService;
import com.wobito.service.IDeviceLogService;
import com.wobito.service.IManagerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @Author Wangbaba
 * @Date 2024/9/2 10:04
 * @Version 1.0
 */

@RestController
@RequestMapping("/operate/manage")
@Api(tags = "管理操作接口")
public class AdminController {

    @Autowired
    private CabinetLatService cabinetLatService;
    @Autowired
    private IManagerService managerService;
    @Autowired
    private BookService bookService;
    @Autowired
    private IDeviceLogService deviceLogService;

    @GetMapping("/verify/{code}")
    @ApiOperation(value = "验证是否是管理员")
    public BaseResponse verifyIsAdmin(@PathVariable("code") String code) {
        int manager = managerService.selectManagerByCode(code);
        deviceLogService.insertDeviceLog(new DeviceLog(null, DateUtils.getNowDate(),null,"验证是否是管理员"));
        return ResultUtils.success(manager > 0 ? "验证通过" : "验证不通过");
    }

    @PostMapping("/adminEnableCabinetLat/{latId}")
    @ApiOperation(value = "根据柜子ID获取所指定柜格(管理员指定开柜)")
    @Transactional(rollbackFor = Exception.class)
    public BaseResponse adminEnableCabinetLats(@Valid @RequestBody AdminOpenLatVO adminOpenLatVO) {
        CabinetLat lat = cabinetLatService.getById(adminOpenLatVO.getLatId());
        cabinetLatService.updateById(new CabinetLat(adminOpenLatVO.getLatId(), lat.getCabinetId(), SysConstants.STATE_TYPE_OPEN, DateUtils.getNowDate()));
        deviceLogService.insertDeviceLog(new DeviceLog(adminOpenLatVO.getLatId(), DateUtils.getNowDate(), adminOpenLatVO.getPersonId(), "根据柜子ID获取所指定柜格(管理员指定开柜)"));
        return ResultUtils.success("柜门状态已开");
    }

    @PostMapping("/adminCloseCabinetLat")
    @ApiOperation(value = "管理员关闭柜门")
    @Transactional(rollbackFor = Exception.class)
    public BaseResponse adminCloseCabinetLat(@Valid @RequestBody AdminCloseLatVO adminCloseLatVO) {
        //修改柜格状态
        cabinetLatService.updateById(new CabinetLat(adminCloseLatVO.getCabinetId(), adminCloseLatVO.getLatId(), SysConstants.STATE_TYPE_CLOSE, DateUtils.getNowDate()));
        //盘点柜格书籍
        bookService.inventoryBooks(adminCloseLatVO.getRfids(), adminCloseLatVO.getLatId(),adminCloseLatVO.getPersonId());
        deviceLogService.insertDeviceLog(new DeviceLog(adminCloseLatVO.getLatId(), DateUtils.getNowDate(), adminCloseLatVO.getPersonId(), "管理员关闭柜门"));
        return ResultUtils.success("操作成功,柜门已关闭");
    }

}
